package site.lyweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import site.lyweb.domain.dto.Course;
import site.lyweb.domain.dto.StuCourseRelation;
import site.lyweb.domain.dto.Student;
import site.lyweb.domain.dto.Teacher;
import site.lyweb.domain.po.StuCoursePo;
import site.lyweb.mapper.CourseMapper;
import site.lyweb.mapper.StuCourseRelationMapper;
import site.lyweb.mapper.StudentMapper;
import site.lyweb.mapper.TeacherMapper;
import site.lyweb.service.StuCourseRelationService;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
* @author 深山老林
* @description 针对表【stu_course_relation】的数据库操作Service实现
* @createDate 2024-06-19 20:53:31
*/
@Service
public class StuCourseRelationServiceImpl extends ServiceImpl<StuCourseRelationMapper, StuCourseRelation>
    implements StuCourseRelationService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private StuCourseRelationMapper stuCourseRelationMapper;

    @Override
    public StuCoursePo getStuCourseById(String id) {
        //根据id查询
        StuCourseRelation stuCourseRelation = stuCourseRelationMapper.selectById(id);
        //获取到几个关联的id
        String stuId = stuCourseRelation.getStuId();
        String courseId = stuCourseRelation.getCourseId();
        String teacherId = stuCourseRelation.getTeacherId();
        //根据查到的id，再去查询几个对象
        Student student = new Student();
        Course course = new Course();
        Teacher teacher = new Teacher();
        //查询
        if(stuId!=null&&!stuId.isEmpty()){
            student = studentMapper.selectById(stuId);
        }
        if(courseId!=null && !courseId.isEmpty()){
            course = courseMapper.selectById(courseId);
        }
        if(teacherId!=null && !teacherId.isEmpty()){
            teacher = teacherMapper.selectById(teacherId);
        }
        //合并对象
        StuCoursePo stuCoursePo = mergeStuCourseTeacher(student, teacher, course);
        System.out.println(stuCoursePo);
        return stuCoursePo;
    }
    public StuCoursePo mergeStuCourseTeacher(Student student,Teacher teacher,Course course){
        StuCoursePo stuCoursePo = new StuCoursePo();
        //学生
        stuCoursePo.setStuId(student.getId());
        stuCoursePo.setStuAge(student.getAge());
        stuCoursePo.setStuName(student.getName());
        stuCoursePo.setStuPhone(student.getPhone());
        //课程
        stuCoursePo.setCourseId(course.getId());
        stuCoursePo.setCoursName(course.getName());
        stuCoursePo.setCourseCredits(course.getCredits());
        //老师
        stuCoursePo.setTeacherId(teacher.getId());
        stuCoursePo.setTeacherName(teacher.getName());
        stuCoursePo.setTeacherAge(teacher.getAge());
        stuCoursePo.setTeacheProfession(teacher.getProfession());

        return stuCoursePo;
    }

    @Override
    public List<StuCoursePo> getStuCourseByStuId(String stuId) {
        List<StuCoursePo> stuCoursePoList = new ArrayList<>();

        // 根据学生 ID 查询关联表获取所有关联的课程 ID
        List<StuCourseRelation> stuCourseRelations = stuCourseRelationMapper.selectByStuId(stuId);

        for (StuCourseRelation stuCourseRelation : stuCourseRelations) {
            String courseId = stuCourseRelation.getCourseId();
            String teacherId = stuCourseRelation.getTeacherId();

            Student student = studentMapper.selectById(stuId);
            Course course = courseMapper.selectById(courseId);
            Teacher teacher = teacherMapper.selectById(teacherId);

            StuCoursePo stuCoursePo = mergeStuCourseTeacher(student, teacher, course);
            stuCoursePoList.add(stuCoursePo);
        }

        return stuCoursePoList;
    }

    @Override
    public List<StuCoursePo> getStuCourseByCourseId(String courseId) {
        List<StuCoursePo> stuCoursePoList = new ArrayList<>();

        // 根据课程 ID 查询关联表获取所有关联的学生 ID
        List<StuCourseRelation> stuCourseRelations = stuCourseRelationMapper.selectByCourseId(courseId);

        for (StuCourseRelation stuCourseRelation : stuCourseRelations) {
            String stuId = stuCourseRelation.getStuId();
            String teacherId = stuCourseRelation.getTeacherId();

            Student student = studentMapper.selectById(stuId);
            Course course = courseMapper.selectById(courseId);
            Teacher teacher = teacherMapper.selectById(teacherId);

            StuCoursePo stuCoursePo = mergeStuCourseTeacher(student, teacher, course);
            stuCoursePoList.add(stuCoursePo);
        }

        return stuCoursePoList;
    }

    @Override
    public boolean queryCourseStuByTeaId(String id) {
        return stuCourseRelationMapper.queryCourseStuByTeaId(id);
    }

    @Override
    public List<StuCoursePo> getStuCourseByTeacherId(String teacherId) {
        List<StuCoursePo> stuCoursePoList = new ArrayList<>();

        // 根据老师ID 查询关联表获取所有关联的课程 ID
        List<StuCourseRelation> stuCourseRelations = stuCourseRelationMapper.selectByTeacherId(teacherId);
        for (StuCourseRelation stuCourseRelation : stuCourseRelations) {
            String courseId = stuCourseRelation.getCourseId();
            String stuId = stuCourseRelation.getStuId();

            Student student = studentMapper.selectById(stuId);
            Course course = courseMapper.selectById(courseId);
            Teacher teacher = teacherMapper.selectById(teacherId);

            StuCoursePo stuCoursePo = mergeStuCourseTeacher(student, teacher, course);
            stuCoursePoList.add(stuCoursePo);
        }

        return stuCoursePoList;
    }

}




